package com.hengda.museumonline.utils;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.adapter.CityChengeAdapter;
import com.hengda.museumonline.adapter.EncAdapter;
import com.hengda.museumonline.adapter.HotCityAdapter;
import com.hengda.museumonline.entity.CityChengeBean;
import com.hengda.museumonline.entity.CityEvent;
import com.hengda.museumonline.entity.LoginBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.rxbus.RxBus;
import com.hengda.museumonline.ui.MainActivity;
import com.hengda.museumonline.ui.SettingActivity;
import com.hengda.museumonline.view.AutoNextLineLinearlayout;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

/**
 * @author：JFWU on 2016/10/12 09:03
 * @E-mail：jeffy12138@126.com
 */
public class CityChengeUtil {
    private PopupWindow mPopWindow;
    private MainActivity mainActivity;
    private RecyclerView recyclerview;
    private RecyclerView rv_city_chenge;
    private CityChengeBean cityChengeBean = null;
    private ImageView close;
    private boolean isShow = false;

    public void dismiss() {
        if (mPopWindow != null && mPopWindow.isShowing()) {
            mPopWindow.dismiss();
        }

    }

    public void getCityFromServer(final MainActivity mainActivity, final LinearLayout cityChenge, String url) {
        if (!isShow) {
            isShow = true;
            Request request = NoHttp.createStringRequest(MyApplication.url + url, RequestMethod.GET);
            request.add("p", "a");
            request.add("v", PackageUtil.getAppPackageInfo(mainActivity).versionName);

            CallServer.getRequestInstance().add(mainActivity, 0, request, new OnResponseListener() {
                @Override
                public void onStart(int what) {
                }

                @Override
                public void onSucceed(int what, Response response) {
                    Gson gson = new Gson();
                    cityChengeBean = gson.fromJson(response.get().toString(), CityChengeBean.class);
                    View contentView = LayoutInflater.from(mainActivity).inflate(R.layout.citychenge_pop, null);
                    close = (ImageView) contentView.findViewById(R.id.close);
                    close.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mPopWindow.dismiss();
                            isShow = false;
                        }
                    });
                    recyclerview = (RecyclerView) contentView.findViewById(R.id.recyclerview);
                    recyclerview.setLayoutManager(new GridLayoutManager(mainActivity, 2));
                    MyHotCityAdapter hotCityAdapter = new MyHotCityAdapter(mainActivity, cityChengeBean);
                    recyclerview.setAdapter(hotCityAdapter);
                    rv_city_chenge = (RecyclerView) contentView.findViewById(R.id.rv_city_chenge);
                    rv_city_chenge.setLayoutManager(new GridLayoutManager(mainActivity, 1));
                    MyCityChengeAdapter adapter = new MyCityChengeAdapter(mainActivity, cityChengeBean);
                    rv_city_chenge.setAdapter(adapter);
                    mPopWindow = new PopupWindow(contentView);
                    mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
                    mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
                   // mPopWindow.showAsDropDown(cityChenge, 0, 20);
                    mPopWindow.showAtLocation(cityChenge, Gravity.BOTTOM, 0, 0);

                }

                @Override
                public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                    System.out.println("onFailed:" + exception);
                }

                @Override
                public void onFinish(int what) {
                }
            });
        }

    }

    class MyHotCityAdapter extends HotCityAdapter {

        public MyHotCityAdapter(Context context, CityChengeBean cityChengeBean) {
            super(context, cityChengeBean);
        }

        @Override
        public void convert(Context mContext, HotCityAdapter.ListHolder holder, final int position) {
            holder.getLl_hotcity().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPopWindow.dismiss();
                    isShow = false;
                    RxBus.get().post("city", new CityEvent(cityChengeBean.getData().getHot().get(position).getName(), Integer.parseInt(cityChengeBean.getData().getHot().get(position).getId())));
                }
            });
            holder.getHotcity().setText(cityChengeBean.getData().getHot().get(position).getName());
        }
    }

    class MyCityChengeAdapter extends CityChengeAdapter {
        public MyCityChengeAdapter(Context context, CityChengeBean cityChengeBean) {
            super(context, cityChengeBean);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, final int position) {
            if (position == 0) {
                for (int i = 0; i < cityChengeBean.getData().getDcity().size(); i++) {
                    View contentView = LayoutInflater.from(mContext).inflate(R.layout.city_item, null);
                    final int finalI = i;
                    contentView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mPopWindow.dismiss();
                            isShow = false;
                            RxBus.get().post("city", new CityEvent(cityChengeBean.getData().getDcity().get(finalI).getName(), Integer.parseInt(cityChengeBean.getData().getDcity().get(finalI).getId())));
                        }
                    });
                    TextView city = (TextView) contentView.findViewById(R.id.city);
                    city.setText(cityChengeBean.getData().getDcity().get(i).getName());
                    ((CityChengeAdapter.FirstHolder) holder).getLinearlayout().addView(contentView);
                }
            } else {
                ((CityChengeAdapter.ListHolder) holder).getProvince().setText(cityChengeBean.getData().getList().get(position - 1).getName());
                for (int i = 0; i < cityChengeBean.getData().getList().get(position - 1).getCity().size(); i++) {
                    View contentView = LayoutInflater.from(mContext).inflate(R.layout.city_item, null);
                    final int finalI = i;
                    contentView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mPopWindow.dismiss();
                            isShow = false;
                            RxBus.get().post("city", new CityEvent(cityChengeBean.getData().getList().get(position - 1).getCity().get(finalI).getName(), Integer.parseInt(cityChengeBean.getData().getList().get(position - 1).getCity().get(finalI).getId())));
                        }
                    });
                    TextView city = (TextView) contentView.findViewById(R.id.city);
                    city.setText(cityChengeBean.getData().getList().get(position - 1).getCity().get(i).getName());
                    ((CityChengeAdapter.ListHolder) holder).getLinearlayout().addView(contentView);
                }
            }
        }
    }

}
